﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using NUnit.Framework;

namespace OrderExer
{
    //订单服务
    public class OrderService
    {
        public List<Order> orderList = new List<Order>();//保存订单数据的List
        //添加订单、删除订单、修改订单
        public void addOrder(Order order)//添加订单
        {

            bool flag = false;
            foreach (Order o in orderList)
            {
                if (order.Equals(o))
                {
                    flag = true;
                    Console.WriteLine("该订单已存在，无法添加");
                    break;
                }
            }
            //如果该订单之前没有被创建过，就添加
            if (!flag)
            {
                orderList.Add(order);
                Console.WriteLine("添加成功");
            }


        }
        public void trydelete(int orderNo)
        {
            bool flag = false;//记录有没有删除订单
            foreach(Order order in orderList)
            {
                if (order.OrderNo == orderNo)
                {
                    flag = true;
                    orderList.Remove(order);
                    Console.WriteLine("删除成功");
                    break;
                }
            }
            if (!flag)
            {
                throw new MyException("订单号错误，未找到该订单，无法删除",1);
            }
        }

        public void deleteOrder(int orderNo)//删除订单，删除失败会抛除异常
        {
            try
            {
                trydelete(orderNo);
            }
            catch(MyException e)
            {
                Console.WriteLine(e.Message+" 出错种类为"+e.Idnumber);
            }
        }

        public void trymodify(int orderNo,string cname)
        {
            bool flag = false;//记录有没有修改订单
            foreach (Order order in orderList)
            {
                if (order.OrderNo == orderNo)
                {
                    flag = true;
                    order.Cname = cname;
                    Console.WriteLine("修改成功");
                    break;
                }
            }
            if (!flag)
            {
                throw new MyException("订单号错误，未找到该订单，无法修改", 2);
            }
        }

        public void modifyOrder(int orderNo,string cname)//修改订单，修改失败会抛除异常
        {
            try
            {
                trymodify(orderNo, cname);
            }
            catch (MyException e)
            {
                Console.WriteLine(e.Message + " 出错种类为" + e.Idnumber);
            }
        }
        public void sort()
        {
            this.orderList.Sort((x, y) => x.OrderNo.CompareTo(y.OrderNo));
        }
        //提供排序方法对保存的订单进行排序。默认按照订单号排序，也可以使用Lambda表达式进行自定义排序。
        public void sort(Func<Order,Order,int> func)
        {
            this.orderList.Sort((x,y)=>func(x,y));
        }

        XmlSerializer xmlser = new XmlSerializer(typeof(Order[]));
        String xmlFileName = "order.xml";//定义xml文件名字
        //将所有的订单序列化为XML文件
        public void Export()
        {

            Order[] temp = new Order[orderList.Count];
            orderList.CopyTo(temp);
            
            //XML序列化
            
            FileStream fs = new FileStream(xmlFileName, FileMode.Create);
            xmlser.Serialize(fs, temp); 
            fs.Close();

            //显示xml文本
            string xml = File.ReadAllText(xmlFileName);
            Console.WriteLine(xml);
        }

        //从XML文件中载入订单
        public void Import()
        {
            using (FileStream fs = new FileStream(xmlFileName, FileMode.Open))
            {
                Order[] temp = (Order[])xmlser.Deserialize(fs);
                Array.ForEach(temp, p => Console.WriteLine(p));
            }

        }
        
        /*
        [Test]
        public void addOrder1()
        {
            
            OrderExer.Order order = new OrderExer.Order(1, "test", "test", 1, 1);
            OrderExer.OrderService service = new OrderExer.OrderService();
            service.addOrder(order);
            List<OrderExer.Order> orderList = new List<OrderExer.Order>();
            orderList.Add(order);
            Assert.AreEqual(orderList, service.orderList);
        }
        [Test]
        public void TestMethod1()
        {
            
            OrderExer.Order order = new OrderExer.Order(1, "test", "test", 1, 1);
            OrderExer.Order order1 = new OrderExer.Order(2, "test2", "test2", 1, 1);
            OrderExer.OrderService service = new OrderExer.OrderService();
            service.addOrder(order);
            service.addOrder(order1);
            service.deleteOrder(2);
            List<OrderExer.Order> orderList = new List<OrderExer.Order>();
            orderList.Add(order);
            Assert.AreEqual(orderList, service.orderList);
           
        }
        */
    }

    
}

